﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Lswweb.Model;
using Lswweb.IDAL;
using Lswweb.Common;

namespace Lswweb.SqlServerDAL
{
    public class LogisticsAccountingDAL : ILogisticsAccountingDAL
    {
        #region 构造方法、字段

        private Builder<LogisticsAccountingInfo> builderDAL = new Builder<LogisticsAccountingInfo>("LogisticsAccounting", "CityId");

        public LogisticsAccountingDAL()
        {
            builderDAL.CreateModel += new ReaderBind<LogisticsAccountingInfo>(builderDAL_CreateModel);
            builderDAL.GetParameters += new ParametersBuilder<LogisticsAccountingInfo>(builderDAL_GetParameters);
        }

        IDataParameter[] builderDAL_GetParameters(LogisticsAccountingInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@CityId",t.CityId),
                DataAccess.CreateParameter("@LogisticsCompanyId",t.LogisticsCompanyId),
                DataAccess.CreateParameter("@Expressions",t.Expressions)
            };
        }

        LogisticsAccountingInfo builderDAL_CreateModel(Hashtable reader)
        {
            LogisticsAccountingInfo entity = new LogisticsAccountingInfo();

            entity.CityId = reader.Get<int>("CityId");
            entity.LogisticsCompanyId = reader.Get<int>("LogisticsCompanyId");
            entity.Expressions = reader.Get<string>("Expressions");

            return entity;
        }

        #endregion

        public LogisticsAccountingInfo FindBy(int cityId,int logisticsCompanyId)
        {
            return builderDAL.GetModel(
                "CityId=@CityId and LogisticsCompanyId=@LogisticsCompanyId",
                "LogisticsCompanyId desc",
                DataAccess.CreateParameter("CityId", cityId),
                DataAccess.CreateParameter("LogisticsCompanyId", logisticsCompanyId));
        }

        public IList<LogisticsAccountingInfo> FindBy(int cityId)
        {
            int total;

            return builderDAL.GetModels(
                string.Empty,
                "CityId=@CityId",
                "LogisticsCompanyId desc",
                1,
                9999,
                out total,
                DataAccess.CreateParameter("@CityId",cityId));
        }

        public IList<LogisticsAccountingInfo> FindBy()
        {
            return builderDAL.GetModels();
        }

        public void Add(LogisticsAccountingInfo entity)
        {
            builderDAL.Insert(
                entity,
                "CityId",
                "LogisticsCompanyId",
                "Expressions");
        }

        public void Update(LogisticsAccountingInfo entity)
        {
            builderDAL.Update(
                entity,
                "CityId=@CityId and LogisticsCompanyId=@LogisticsCompanyId",
                "Expressions");
        }

        public bool IsHas(int cityId,int logisticsCompanyId)
        {
            return builderDAL.IsHas(
                "CityId=@CityId and LogisticsCompanyId=@LogisticsCompanyId",
                DataAccess.CreateParameter("CityId",cityId),
                DataAccess.CreateParameter("LogisticsCompanyId",logisticsCompanyId));
        }

        public void Delete(int id)
        {
            builderDAL.Delete(id);
        }
    }
}
